﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Model;
using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;

namespace MT.eVotePRS.UI.UserControls
{
    public partial class HandsetMaintain : UserControl, IViewHandsetMaintain
    {
        #region IViewHandsetMaintain

        Handsets _handset;
        InventoryTxn _txn;

        public Handsets Handset
        {
            get { return _handset; }
            set {
                _handset = value;
                txtHandsetID .Text = _handset.MAC;
            }
        }

        public InventoryTxn Txn
        {
            set { _txn = value; }
        }

        public string EventRemark
        {
            get { return txtRemark.Text; }
        }

        public event EventHandler HandsetEntered;
        public event EventHandler ConfirmClicked;

        public void SetError(string propertyName, string errorMessage)
        {

        }

        public void EnableConfirm()
        {
            this.btnConfirm.Enabled = true;
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public HandsetMaintain()
        {
            InitializeComponent();

            this.txtHandsetID.KeyDown += OnKeyDown;
            this.btnConfirm.Click += OnConfirmClicked;

            IList<Status> statusList = new List<Status>();
            statusList.Add(Presentation.Util.Constants.Status_Nil);
            statusList.Add(Presentation.Util.Constants.Status_CheckOut);
            statusList.Add(Presentation.Util.Constants.Status_CheckIn);
            statusList.Add(Presentation.Util.Constants.Status_Lost);
            statusList.Add(Presentation.Util.Constants.Status_Fault);
            statusList.Add(Presentation.Util.Constants.Status_Found);
            statusList.Add(Presentation.Util.Constants.Status_Repaired);
            statusList.Add(Presentation.Util.Constants.Status_FoundnFault);

            cmbBoxStatus.DataSource = statusList;
            cmbBoxStatus.DisplayMember = "StatusDesc";

        }
        
        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtHandsetID.Text))
                {
                    _handset.MAC = txtHandsetID.Text;

                    EventHandler handlers = HandsetEntered;
                    if (handlers != null)
                    {
                        handlers(this, EventArgs.Empty);
                    }
                }
            }
        }

        void OnConfirmClicked(object sender, EventArgs e)
        {
            EventHandler handlers = ConfirmClicked;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        
    }
}
